#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->treeWidget->setHeaderLabel("动物");
    ui->treeWidget->setColumnCount(2);

    QTreeWidgetItem* item1 = new QTreeWidgetItem();
    item1->setText(0,"大猫");
    item1->setText(1,"工藤小猫");
    ui->treeWidget->addTopLevelItem(item1);

    QTreeWidgetItem* item2 = new QTreeWidgetItem();
    item2->setText(0,"大狗");
    item2->setText(1,"工藤小狗");
    ui->treeWidget->addTopLevelItem(item2);

    QTreeWidgetItem* item3 = new QTreeWidgetItem();
    item3->setText(0,"羊");
    ui->treeWidget->addTopLevelItem(item3);

    ui->lineEdit->setPlaceholderText("请输入文本");
    ui->lineEdit->setClearButtonEnabled(true);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_new_top_clicked()
{
    const QString & tmp = ui->lineEdit->text();
    if(tmp.isEmpty())
    {
        return;
    }
    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0,tmp);
    ui->treeWidget->addTopLevelItem(item);

}

void Widget::on_pushButton_new_current_clicked()
{
    const QString& tmp = ui->lineEdit->text();
    if(tmp.isEmpty())
    {
        return;
    }
    QTreeWidgetItem* current = ui->treeWidget->currentItem();
    if(current==nullptr)
    {
        return;
    }
    QTreeWidgetItem* insert = new QTreeWidgetItem();
    insert->setText(0,tmp);
    current->addChild(insert);
    current->setExpanded(true);
}
